package com.china08.yxyapi.api.manage.stu.myclass.moments;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.moments.YxyMoments;
import com.china08.yxyapi.entity.mg.moments.YxyMomentsComment;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsCommentRepository;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsRepository;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

/**
 * 班级圈--家长端--删除评论
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class MomentsStuDelCommentController extends YxyApiBaseController {

    @Autowired
    private YxyMomentsCommentRepository yxyMomentsCommentRepository;

    @Autowired
    private YxyMomentsRepository yxyMomentsRepository;

    @Autowired
    private YxUserPointsUtilV3 yxUserPointsUtilV3;

    @RequestMapping(value = "/api_yw/manage/stu/myclass/moments/stuDelComment/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel precess(HttpServletRequest request,
            HttpServletResponse response, RespModel respModel,
            @PathVariable("id") String id) {
        String userId = getProfile(request, response).getId();

        // 查一下有无此评论
        YxyMomentsComment comment = yxyMomentsCommentRepository
                .findByIdAndUserId(id, userId);
        if (comment != null) {
            String momentsId = comment.getMomentsId();
            yxyMomentsCommentRepository.delete(comment);

            // 查对应班级圈的评论
            YxyMoments moments = yxyMomentsRepository.findById(momentsId);
            if (moments != null) {
                Integer count = yxyMomentsCommentRepository
                        .countByMomentsId(momentsId);
                moments.setCommentCount(count);
                yxyMomentsRepository.save(moments);
            }

        }
        yxUserPointsUtilV3.delCommentClassMessage(userId);
        return new YxyApiRespModel("删除成功");
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
